﻿using UnityEngine;
using UnityEngine.SceneManagement;
using System.Diagnostics;
using UnityEngine.UI;
using System.Collections;
using System.Xml;
using System.Collections.Generic;

public class tBuilding5Scene : MonoBehaviour
{
    /// <summary>
    /// This script should be set for cardboard camera.
    /// It can control the sphere of the movie. The picture after the movie is also controled by this script.
    /// </summary>
    MediaPlayerCtrl m_Media;
    int movieID = 0;
    bool isPlay = true;
    Stopwatch time = new Stopwatch();
    public int playTime;// The time of movie
    public string quitScene;// Quit for next scene
    public string museumScene = "museumScene";
    //public string tBuilding5Scene = "tBuilding5Scene";

    objectHide messageQuit;
    objectHide messageButton;

    // Use this for initialization
    void Start()
    {
        // Get m_Media of playing movies
        m_Media = GameObject.FindGameObjectWithTag("mSphere").GetComponent<MediaPlayerCtrl>();
        messageQuit = new objectHide(GameObject.Find("messageQuit"));
        messageButton = new objectHide(GameObject.Find("messageButton"));

        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("mSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, 0, 0);
            time.Start();
        }
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("pSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, -100, 0);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (time.ElapsedMilliseconds >= playTime * 1000 && isPlay)
        {
            isPlay = false;
            time.Stop();
            MovieChangeToPhoto();
        }
    }

    private void MovieChangeToPhoto()
    {
        // Change the position of sphere
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("pSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, 0, 0);
        }
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("mSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, -100, 0);
        }

        // Buttons should be active
        ChangeHiddenOfButton(messageButton);
        ChangeHiddenOfButton(messageQuit);
    }

    //加载视频
    public void Load()
    {
        time.Reset();
        time.Start();
        m_Media.Load(m_Media.m_strFileName);
    }

    //播放视频
    public void Play()
    {
        time.Start();
        m_Media.Play();
    }

    //暂停视频
    public void Pause()
    {
        time.Stop();
        m_Media.Pause();
    }

    // Quit this scene
    public void Quit()
    {
        SceneManager.LoadScene(quitScene);
    }

    // Go to the library scene
    public void MuseumScene()
    {
        SceneManager.LoadScene(this.museumScene);
    }

    public void Library()
    {
        ChangeHiddenOfButton(messageButton);
        ChangeHiddenOfButton(messageQuit);
        GameObject.Find("messageButton").GetComponentInChildren<Text>().text = GetTextInfomationFromService(4);
    }

    public void tBuilding5()
    {
        ChangeHiddenOfButton(messageButton);
        ChangeHiddenOfButton(messageQuit);
        GameObject.Find("messageButton").GetComponentInChildren<Text>().text = GetTextInfomationFromService(5);
    }
    // If the button is active, then hide it. Vice versa
    public void ChangeHiddenOfButton(objectHide btnObj)
    {
        Vector3 position = new Vector3();
        if (btnObj.hide)
        {
            btnObj.hide = !btnObj.hide;
            position = btnObj.obj.transform.position;
            position.z *= 1000;
            btnObj.obj.transform.position = position;
        }
        else
        {
            btnObj.hide = !btnObj.hide;
            position = btnObj.obj.transform.position;
            position.z /= 1000;
            btnObj.obj.transform.position = position;
        }
    }

    string GetTextInfomationFromService(int id)
    {
        string baseUrl = "http://112.126.69.154:8090/iserver/services/data-WHUvr360/rest/data/datasources/name/VR360/datasets/name/Site/features/{0}.xml";
        string url = string.Format(baseUrl, id);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(url);

        Dictionary<string, string> result = new Dictionary<string, string>();

        XmlNode valueNodes = xmlDoc.SelectSingleNode("Feature/fieldValues");

        XmlNodeList valueChilds = valueNodes.ChildNodes;
        XmlNode nameNodes = xmlDoc.SelectSingleNode("Feature/fieldNames");

        XmlNodeList nameChilds = nameNodes.ChildNodes;

        if (valueChilds.Count != nameChilds.Count)
        {
            throw new XmlException("格式错误！");
        }

        for (int i = 0; i < valueChilds.Count; i++)
        {
            if (nameChilds[i].FirstChild.Value == "SITEPROPERTY"
                || nameChilds[i].FirstChild.Value == "SITENAME")
            {
                result.Add(nameChilds[i].FirstChild.Value, valueChilds[i].FirstChild.Value);
            }
        }
        return result["SITEPROPERTY"];
    }

    public class objectHide// Control the activitation of the button
    {
        public GameObject obj { set; get; }
        public bool hide { set; get; }
        public objectHide(GameObject obj)
        {
            this.obj = obj;
            hide = false;
        }
    }
}
